#include "gagent_def.h"

static MQTTClient gagent_mqtt;

extern con_st con;
extern cloud_st *cloud;
extern lan_st *lan;




static void gagent_mqtt_callback(MQTTClient *c, MessageData *msg_data)
{
    rt_kprintf("%s\n", __FUNCTION__);
    rt_kprintf("mqtt_callback topic_name: %d %s\n", msg_data->topicName->lenstring.len, msg_data->topicName->lenstring.data);
    rt_kprintf("data_len:%d ", msg_data->message->payloadlen);

    size_t len;
    char *data = (char *)msg_data->message->payload;

    for(len = 0; len < msg_data->message->payloadlen; len ++)
    {
        rt_kprintf("%02x ", data[len]);
    }
    rt_kprintf("\r\n");


}


static void gagent_mqtt_connect_callback(MQTTClient *c)
{
    rt_kprintf("%s\n", __FUNCTION__);
}

static void gagent_mqtt_online_callback(MQTTClient *c)
{
    rt_kprintf("%s\n", __FUNCTION__);

}

static void gagent_mqtt_offline_callback(MQTTClient *c)
{
    rt_kprintf("%s\n", __FUNCTION__);

}

int gagent_mqtt_init(cloud_st *cloud)
{
    RT_ASSERT(cloud != RT_NULL);


    memset(&gagent_mqtt, 0, sizeof(gagent_mqtt));
    
    gagent_mqtt.host = cloud->mqtt_server;
    gagent_mqtt.port = cloud->mqtt_port;

    
    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
    memcpy(&gagent_mqtt.condata, &data, sizeof(MQTTPacket_connectData));
    gagent_mqtt.condata.keepAliveInterval = 30;
    gagent_mqtt.condata.MQTTVersion = 3;
    gagent_mqtt.condata.cleansession = 1;
    gagent_mqtt.condata.clientID.cstring = cloud->con->did;
    gagent_mqtt.condata.username.cstring = cloud->con->did;
    gagent_mqtt.condata.password.cstring = cloud->con->passcode;
    
    gagent_mqtt.buf_size = gagent_mqtt.readbuf_size = 1024;
    gagent_mqtt.buf = malloc(gagent_mqtt.buf_size);
    gagent_mqtt.readbuf = malloc(gagent_mqtt.readbuf_size);
    if(!(gagent_mqtt.readbuf && gagent_mqtt.readbuf))
    {
        rt_kprintf("mqtt malloc failed!\n");
        return -RT_ENOMEM;
    }

    gagent_mqtt.connect_callback = gagent_mqtt_connect_callback;
    gagent_mqtt.online_callback = gagent_mqtt_online_callback;
    gagent_mqtt.offline_callback = gagent_mqtt_offline_callback;

    memset(cloud->sub_topic[0], 0, sizeof(cloud->sub_topic[0]));
    snprintf(cloud->sub_topic[0], 128, "ser2cli_res/%s", cloud->con->did);
    gagent_mqtt.messageHandlers[0].topicFilter = cloud->sub_topic[0];
    gagent_mqtt.messageHandlers[0].callback = gagent_mqtt_callback;

    memset(cloud->sub_topic[1], 0,sizeof(cloud->sub_topic[1]));
    snprintf(cloud->sub_topic[1], 128, "app2dev/%s/#", cloud->con->did);
    gagent_mqtt.messageHandlers[1].topicFilter = cloud->sub_topic[1];
    gagent_mqtt.messageHandlers[1].callback = gagent_mqtt_callback;

    gagent_mqtt.defaultMessageHandler = gagent_mqtt_callback;
    
    rt_kprintf("host:%s port:%d\n", gagent_mqtt.host, gagent_mqtt.port);
    rt_kprintf("clientID:%s username:%s password:%s\n", 
                    gagent_mqtt.condata.clientID.cstring, 
                    gagent_mqtt.condata.username.cstring,
                    gagent_mqtt.condata.password.cstring);
    rt_kprintf("topic:%s\n", gagent_mqtt.messageHandlers[0].topicFilter);
    
    
    return paho_mqtt_start(&gagent_mqtt);
}


